﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ImageHandler.Framework
{
    public class ReflectionHelper
    {
        public static List<Type> GetDerivedTypes<TBase>()
        {
            var baseType = typeof(TBase);
            var assembly = Assembly.GetAssembly(baseType);

            return assembly.GetTypes()
                           .Where(t => t.IsSubclassOf(baseType) && !t.IsAbstract)
                           .ToList();
        }

        public static T CreateInstance<T>(string className, params object[] constructorArgs) where T : class
        {
            // Get the type of the class
            var type = Assembly.GetExecutingAssembly().GetType(className);
            if (type == null)
            {
                throw new ArgumentException($"Type '{className}' not found in the current assembly.");
            }

            // Ensure the type is a subclass of the specified base class
            if (!typeof(T).IsAssignableFrom(type))
            {
                throw new ArgumentException($"Type '{className}' is not a subclass of '{typeof(T).Name}'.");
            }

            // Create an instance of the class
            return Activator.CreateInstance(type, constructorArgs) as T;
        }
    }
}
